require'luasql.mysql'
require'zdc.ds.dbutil'
require'zdc.ds.dbsub'
module(..., package.seeall)

local schemas = {}
schemas.sqlex_limit="LIMIT %s"

schemas.node = [[
CREATE TABLE IF NOT EXISTS $tablename ( 
	id VARCHAR(255) NOT NULL,
	pid VARCHAR(255),
	ik INT,
	ts INT,
	data MEDIUMBLOB NOT NULL,
	PRIMARY KEY(id)
) DEFAULT CHARSET=utf8;]]

schemas.label = [[
CREATE TABLE IF NOT EXISTS %s ( 
	id VARCHAR(255) NOT NULL,
	idx INT,
	sum VARCHAR(255),
	timestamp VARCHAR(32),
	usr VARCHAR(255),
	PRIMARY KEY(id)
)DEFAULT CHARSET=utf8;]]

local _mysql = {}
local _mysql_mt = {__metatable = {}, __index = _mysql}

zdc.ds.dbutil.MakeInhMT(_mysql)

function new(params)
	local env = luasql.mysql()
	local obj = {env=env,con=con,params=params,tablenode=params.tablenode}
	setmetatable(obj, _mysql_mt)
	obj:onrestore()
	return obj._sub[self.params.tablenode or "node"],obj
end

function _mysql:subdb(tablenode)
	return zdc.ds.dbsub.new(self,schemas,tablenode)
end

function _mysql:init()
	self:subdb(self.params.tablenode or "node")
end

function _mysql:fmt_type(t)
	return t or 'MEDIUMBLOB'
end
